/**
 * Created by WT-SUN on 2017/5/2.
 */

$(function () {
    var arrTaskObj=WindowManager.getParams();
    var arrTask;
    for (var i in arrTaskObj){
        arrTask= arrTaskObj[i];
        break;
    }
    $('#btn_save').on('click', function () {
        submitForm();
    });
    taskTb(arrTask);
    initTb();
});

var editIndex_taskTb = undefined;
function endEditingRtb(){
    if (editIndex_taskTb == undefined){return true}
    if ($('#taskTb').datagrid('validateRow', editIndex_taskTb)){
        $('#taskTb').datagrid('endEdit', editIndex_taskTb);
        editIndex_taskTb = undefined;
        return true;
    } else {
        return false;
    }
}

function initTb() {
    $('#taskTb_save').on('click', function () {
        if (endEditingRtb()){
        }
    });
    $('#taskTb_remove').on('click', function () {
        if (editIndex_taskTb == undefined){return}
        $('#taskTb').datagrid('cancelEdit', editIndex_taskTb).datagrid('deleteRow', editIndex_taskTb);
        editIndex_taskTb = undefined;
    });
}
function taskTb(arrTask) {
    $('#taskTb').datagrid({
        fitColumns: true,
        idField: 'id',
        rownumbers: true,
        data:arrTask,
        sortOrder: true,
        singleSelect: true,
        striped: true,
        fit: false,
        height: 400,
        columns: [[
            {
                field: 'id', title: 'id', hidden:true,width: 20
            },
            {
                field: 'taskId', hidden:true, title: '业务Id', hidden:true,width: 20
            },
            {
                field: 'taskSource', hidden:true, title: '来源', hidden:true,width: 20
            },
            {field: 'taskName', title: '任务名称', width: 120,  editor: { type: 'textbox',options:{required:true} }},
            {field: 'taskDescribe', title: '任务描述', width: 80,  editor: { type: 'textbox',options:{required:true} }},
            {field: 'receiveUserId', hidden:true,title: '接收人Id', width: 100,editor: {type: 'textbox',options:{editable:false}}},
            {field: 'receiveUserName', title: '接收人', width: 100,
                editor: {
                    type: 'textbox',
                    options:{
                        iconWidth: 20,
                        editable:false,
                        required:true,
                        icons: [{
                            iconCls: 'icon-add',
                            handler: function(e){
                                add_userId_funcRe('receiveUser');
                            }
                        },{
                            iconCls: 'icon-remove',
                            handler: function(e){
                                $(e.data.target).textbox('clear');
                                var edId = $("#taskTb").datagrid('getEditor', { index: editIndex_taskTb, field: 'receiveUserId' });
                                $(edId.target).textbox('setValue', '').textbox('setText', '');
                                var edDeId = $("#taskTb").datagrid('getEditor', { index: editIndex_taskTb, field: 'jsDeptName' });
                                $(edDeId.target).textbox('setValue', '').textbox('setText', '');
                            }
                        }]
                    }
                }},
            {field: 'jsDeptName', title: '接收单位', width: 100,editor: {type: 'textbox',options:{editable:false,required:true}}},
            {field: 'requireTime', title: '要求完成日期', width: 100,editor: {type: 'datebox', options: {required: true,editable:false}}},
            {field: 'acceptUserId', hidden:true,title: '验收人Id', width: 100,editor: {type: 'textbox',options:{editable:false}}},
            {field: 'acceptUserName', title: '验收人', width: 100,
                editor: {
                    type: 'textbox',
                    options:{
                        iconWidth: 20,
                        editable:false,
                        required:true,
                        icons: [{
                            iconCls: 'icon-add',
                            handler: function(e){
                                add_userId_funcAc();
                            }
                        },{
                            iconCls: 'icon-remove',
                            handler: function(e){
                                $(e.data.target).textbox('clear');
                                var edId = $("#taskTb").datagrid('getEditor', { index: editIndex_taskTb, field: 'acceptUserId' });
                                $(edId.target).textbox('setValue', '').textbox('setText', '');
                                var edDeId = $("#taskTb").datagrid('getEditor', { index: editIndex_taskTb, field: 'ysDeptName' });
                                $(edDeId.target).textbox('setValue', '').textbox('setText', '');
                            }
                        }]
                    }
                }},
            {field: 'ysDeptName', title: '验收单位', width: 100,editor: {type: 'textbox',options:{editable:false,required:true}}}

        ]],
        onClickRow:function (index, row) {
            if (editIndex_taskTb != index){
                if (endEditingRtb()){
                    $('#taskTb').datagrid('selectRow', index).datagrid('beginEdit', index);
                    editIndex_taskTb = index;
                }else {
                    $('#taskTb').datagrid('selectRow', editIndex_taskTb);
                }
            }
        }
    });
}

var add_userId_funcRe = function (target) {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            receiveUser(data);
        }
    });
};
function receiveUser(data) {
    var deptName='';
    if (data[0].userDepartmentList && data[0].userDepartmentList.length > 0) {
        for (var index in data[0].userDepartmentList) {
            if (data[0].userDepartmentList[index].deptName != null) {
                deptName += ',' + data[0].userDepartmentList[index].deptName;
            }
        }
        deptName= deptName.substring(1);
    } else {
        deptName='';
    }
    var ed = $("#taskTb").datagrid('getEditor', { index: editIndex_taskTb, field: 'receiveUserName' });
    var edId = $("#taskTb").datagrid('getEditor', { index: editIndex_taskTb, field: 'receiveUserId' });
    $(ed.target).textbox('setValue', data[0].realName).textbox('setText', data[0].realName);
    $(edId.target).textbox('setValue', data[0].id).textbox('setText', data[0].id);
    var edDe = $("#taskTb").datagrid('getEditor', { index: editIndex_taskTb, field: 'jsDeptName' });
    $(edDe.target).textbox('setValue', deptName).textbox('setText', deptName);

}
var add_userId_funcAc = function (target) {//
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            acceptUser(data);
        }
    });
};
function acceptUser(data) {
    var deptName='';
    if (data[0].userDepartmentList && data[0].userDepartmentList.length > 0) {
        for (var index in data[0].userDepartmentList) {
            if (data[0].userDepartmentList[index].deptName != null) {
                deptName += ',' + data[0].userDepartmentList[index].deptName;
            }
        }
        deptName= deptName.substring(1);
    } else {
        deptName='';
    }
    var ed = $("#taskTb").datagrid('getEditor', { index: editIndex_taskTb, field: 'acceptUserName' });
    var edId = $("#taskTb").datagrid('getEditor', { index: editIndex_taskTb, field: 'acceptUserId' });
    $(ed.target).textbox('setValue', data[0].realName).textbox('setText', data[0].realName);
    $(edId.target).textbox('setValue', data[0].id).textbox('setText', data[0].id);
    var edDe = $("#taskTb").datagrid('getEditor', { index: editIndex_taskTb, field: 'ysDeptName' });
    $(edDe.target).textbox('setValue', deptName).textbox('setText', deptName);
}

/**
 * 保存数据
 * @returns {jQuery}
 */
function submitForm() {
    if (endEditingRtb()){
        //$('#taskTb').datagrid('acceptChanges');
    }
    var objArrF=$('#taskTb').datagrid('getData');
    if(objArrF.total==0){
        layer.alert("请正选择业务数据！");
        return;
    }
    for (var i=0;i<objArrF.total;i++){
        var rowData=objArrF.rows[i];
        if (typeof (rowData.taskName)=='undefined'||typeof (rowData.receiveUserName)=='undefined'||typeof (rowData.taskDescribe)=='undefined'||typeof (rowData.acceptUserName)=='undefined'||typeof (rowData.requireTime)=='undefined'){
            layer.alert("请正确填写任务数据！");
            return;
        }
        if (rowData.taskName==''||rowData.receiveUserName==''||rowData.taskDescribe==''||rowData.acceptUserName==''||rowData.requireTime==''){
            layer.alert("请正确填写任务数据！");
            return;
        }
    }

    var objParams={};
    var taskTb= JSON.stringify(objArrF.rows);
    objParams.taskTb=taskTb;

    $.ajax({
        url:ROOT_PATH + '/task/insertData.shtml',//提交地址
        data:objParams,
        type:"POST",
        dataType:"json",
        success: function (result) {
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000
                }, function () {
                    WindowManager.closeLayer({
                        returnValue:true
                    });

                    /*parent.layer.close(parent.layer.getFrameIndex(window.name));
                    $.getIframeWindow().reLoadData();*/
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}


